/*
 * @Author: 抱著羊啃 1437670973@qq.com
 * @Date: 2025-06-02 14:47:54
 * @LastEditors: 抱著羊啃 1437670973@qq.com
 * @LastEditTime: 2025-06-05 19:27:55
 * @FilePath: \Ai_Learn\src\main\java\com\baozheyangken\ai_learn\mapper\QuestionMapper.java
 * @Description: 這是默認設定,請設定`customMade`, 打開koroFileHeader查看配置 進行設定: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
package com.baozheyangken.ai_learn.mapper;

import com.baozheyangken.ai_learn.pojo.entity.Question;
import com.baozheyangken.ai_learn.pojo.entity.QuestionType;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

@Mapper
public interface QuestionMapper {

    // 新增题目
    @Insert("INSERT INTO questions (content, type_id, difficulty_id, user_id, explanation) " +
            "VALUES (#{content}, #{typeId}, #{difficultyId}, #{userId}, #{explanation})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addQuestion(Question question);

    // 根据ID删除题目
    @Delete("DELETE FROM questions WHERE id = #{id}")
    int deleteQuestionById(Integer id);

    // 更新题目
    @Update("UPDATE questions SET content = #{content}, type_id = #{typeId}, " +
            "difficulty_id = #{difficultyId}, explanation = #{explanation} " +
            "WHERE id = #{id}")
    int updateQuestion(Question question);

    // 根据ID查询题目（含关联信息）
    @Select("SELECT q.*, t.type_name AS type_name, d.level_name AS difficulty_name " +
            "FROM questions q " +
            "LEFT JOIN question_types t ON q.type_id = t.id " +
            "LEFT JOIN difficulty_levels d ON q.difficulty_id = d.id " +
            "WHERE q.id = #{id}")
    Question getQuestionById(Integer id);

    // 查询所有题目（支持条件过滤）——现在从 XML 加载
    List<Question> getAllQuestions(Map<String, Object> params);

    // 管理知识点关联
    @Insert("INSERT INTO question_knowledge (question_id, knowledge_id) VALUES (#{questionId}, #{knowledgeId})")
    void insertQuestionKnowledge(int questionId, int knowledgeId);

    @Select("SELECT COUNT(*) FROM knowledges WHERE id = #{knowledgeId}")
    int checkKnowledgeIdExists(int knowledgeId);

    @Insert("INSERT INTO question_knowledge (question_id, knowledge_id) VALUES (#{questionId}, #{knowledgeId})")
    int addQuestionKnowledge(Integer questionId, Integer knowledgeId);

    @Delete("DELETE FROM question_knowledge WHERE question_id = #{questionId}")
    void deleteQuestionKnowledge(Integer questionId);
    
    // 根据知识点ID查询题目
    List<Question> getQuestionsByKnowledgeId(Integer knowledgeId);
    
    // 根据知识点ID和题目类型查询题目（用于智能测试）
    List<Question> getQuestionsByKnowledgeIdAndType(@Param("knowledgeId") Integer knowledgeId, 
                                                   @Param("typeId") Integer typeId, 
                                                   @Param("limit") Integer limit);
    
    // 根据知识点ID、题目类型和难度查询题目（用于智能测试按难度分配）
    List<Question> getQuestionsByKnowledgeIdTypeAndDifficulty(@Param("knowledgeId") Integer knowledgeId, 
                                                             @Param("typeId") Integer typeId, 
                                                             @Param("difficultyId") Integer difficultyId,
                                                             @Param("limit") Integer limit);

    // 根据课程ID和题目类型、难度查询题目（用于智能测试）
    List<Question> getQuestionsByCourseIdTypeAndDifficulty(@Param("courseId") Integer courseId, 
                                                           @Param("typeId") Integer typeId, 
                                                           @Param("difficultyId") Integer difficultyId, 
                                                           @Param("limit") Integer limit);

    // 根据课程ID和题目类型查询题目
    List<Question> getQuestionsByCourseIdAndType(@Param("courseId") Integer courseId, 
                                                 @Param("typeId") Integer typeId, 
                                                 @Param("limit") Integer limit);

    List<QuestionType> getQuestionType();

}